<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreCouponIssue extends Model
{
    use HasFactory;

    protected $guarded = [];
    protected $table = 'store_coupon_issue';


    /**
     * szk
     * 优惠券附表添加
     * @discountData 增加优惠卷需要的数据
     * @param $attachedData
     * @return mixed
     */
    public static function create($attachedData)
    {
        $attachedData['start_time'] = strtotime($attachedData['start_time']);
        $attachedData['end_time'] = strtotime($attachedData['end_time']);
        $attachedData['remain_count'] = $attachedData['total_count'];
        $attachedData['add_time'] = strtotime(date('Y-m-d H:i:s'));
        $attachedData['is_del'] = 0;
        return self::insertGetId($attachedData);
    }

    /**
     * szk
     * 删除主表跟踪附表信息
     * @param $id
     * @return bool
     */
    public static function discountDeleted($id)
    {
        $data = self::where('cid', $id)->first();
        if ($data) {
            return self::where('cid', $id)->delete();
        }
        return true;
    }

    /**
     * szk
     * 优惠券附表修改
     * @param $attachedData
     * @param $id
     * @return mixed
     */
    public static function modification($attachedData, $id)
    {
        $attachedData['start_time'] = strtotime($attachedData['start_time']);
        $attachedData['end_time'] = strtotime($attachedData['end_time']);
        return self::where('cid', $id)->update($attachedData);
    }

    /**
     * szk
     * 优惠券添加时间 时间改变获取器
     * @param $value
     * @return false|string
     */
    public function getAddTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * szk
     * 优惠券领取开启时间 时间改变获取器
     * @param $value
     * @return false|string
     */
    public function getStartTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * szk
     * 优惠券领取结束时间 时间改变获取器
     * @param $value
     * @return false|string
     */
    public function getEndTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }



}
